home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / misc / unix / tracker_4_3.lzh / tracker / split.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-13  |  3.3 KB  |  176 lines

  1. /* split.c 
  2.     vi:se ts=3 sw=3:
  3.  */
  4.  
  5.  
  6. /* $Id: split.c,v 4.1 1994/01/12 16:11:07 espie Exp espie $
  7.  * $Log: split.c,v $
  8.  * Revision 4.1  1994/01/12  16:11:07  espie
  9.  * Last minute changes.
  10.  *
  11.  * Revision 4.0  1994/01/11  17:56:24  espie
  12.  * Cleaner.
  13.  *
  14.  * Revision 1.5  1994/01/09  17:36:22  Espie
  15.  * Generalized open.c.
  16.  *
  17.  * Revision 1.4  1994/01/08  03:55:43  Espie
  18.  * run_in_fg() and create_notes_table() not needed !
  19.  *
  20.  * Revision 1.3  1994/01/06  22:32:42  Espie
  21.  * Use new pref scheme.
  22.  *
  23.  * Revision 1.2  1994/01/05  14:54:09  Espie
  24.  * *** empty log message ***
  25.  *
  26.  * Revision 1.1  1993/12/26  00:55:53  Espie
  27.  * Initial revision
  28.  *
  29.  * Revision 1.2  1993/12/04  16:12:50  espie
  30.  * BOOL -> boolean.
  31.  *
  32.  * Revision 1.1  1993/12/02  15:45:33  espie
  33.  * Initial revision
  34.  *
  35.  */
  36.      
  37.  
  38. #include <stdio.h>
  39. #include <signal.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #ifndef AMIGA
  43. #include <malloc.h>
  44. #endif
  45.      
  46. #include "defs.h"
  47. #include "song.h"
  48. #include "extern.h"
  49.      
  50. #include "getopt.h"
  51.      
  52. ID("$Id: split.c,v 4.1 1994/01/12 16:11:07 espie Exp espie $")
  53.  
  54. /* global variable to catch various types of errors
  55.  * and achieve the desired flow of control
  56.  */
  57. int error;
  58.  
  59. LOCAL struct song *do_scan_song(name, type)
  60. char *name;
  61. int type;
  62.    {
  63.    struct song *song;
  64.    struct exfile *file;
  65.  
  66.    file = open_file(name, "r", getenv("MODPATH"));
  67.    if (!file)
  68.       return NULL;
  69.    song = read_song(file, type); 
  70.    close_file(file);
  71.    return song;
  72.    }
  73.  
  74. #define CHUNK_SIZE 32000
  75.  
  76. LOCAL char *suffix[] =
  77.    {
  78.    "lzh", "lha", "Z", "z", "shn", "zoo", 0
  79.    };
  80.  
  81. LOCAL void truncate(name)
  82. char *name;
  83.    {
  84.    int i;
  85.    int last_point = 0;
  86.  
  87.    for (i = 0; name[i]; i++)
  88.       {
  89.       if (name[i] == '.')
  90.          last_point = i + 1;
  91.       }
  92.    if (last_point)
  93.       {
  94.       for (i = 0; suffix[i]; i++)
  95.       if (strcmp(name + last_point, suffix[i]) == 0)
  96.          {
  97.          name[last_point - 1] = 0;
  98.          return;
  99.          }
  100.       }
  101.    }
  102.  
  103.    
  104.    
  105. void split_module(name, cutpoint)
  106. char *name;
  107. long cutpoint;
  108.    {
  109.    char buffer[300];
  110.    FILE *mod;
  111.    FILE *samp;
  112.    struct exfile *file;
  113.    char *copy_buff;
  114.    int chunk;
  115.  
  116.    file = open_file(name, "r", getenv("MODPATH"));
  117.    truncate(name);
  118.    sprintf(buffer, "%s.mod", name);
  119.    mod = fopen(buffer, "w");
  120.    if (!mod)
  121.       exit(10);
  122.    sprintf(buffer, "%s.samp", name);
  123.    samp = fopen(buffer, "w");
  124.    if (!samp)
  125.       exit(10);
  126.    copy_buff = malloc(CHUNK_SIZE);
  127.    if (!copy_buff)
  128.       exit(10);
  129.    while(cutpoint >= CHUNK_SIZE)
  130.       {
  131.       fread(copy_buff, 1, CHUNK_SIZE, file_handle(file));
  132.       fwrite(copy_buff, 1, CHUNK_SIZE, mod);
  133.       cutpoint -= CHUNK_SIZE;
  134.       }
  135.    if (cutpoint > 0)
  136.       {
  137.       fread(copy_buff, 1, cutpoint, file_handle(file));
  138.       fwrite(copy_buff, 1, cutpoint, mod);
  139.       }
  140.    fclose(mod);
  141.    while ((chunk = fread(copy_buff, 1, CHUNK_SIZE, file_handle(file))) > 0)
  142.       fwrite(copy_buff, 1, chunk, samp);
  143.    fclose(samp);
  144.    close_file(file);
  145.    }
  146.       
  147.    
  148.  
  149.  
  150. int main(argc, argv)
  151. int argc;
  152. char **argv;
  153.    {
  154.    struct song *song;
  155.    int i;
  156.    int default_type;
  157.  
  158.  
  159.    default_type = BOTH;
  160.  
  161.    for (i = 1; i < argc; i++)
  162.       {
  163.       song = do_scan_song(argv[i], NEW);
  164.       if (!song && error != NEXT_SONG)
  165.          song = do_scan_song(argv[i], OLD);
  166.       if (song)
  167.          {
  168.          dump_song(song); 
  169.          split_module(argv[i], song->samples_start);
  170.          release_song(song);
  171.          }
  172.       }
  173.    }
  174.  
  175.  
  176.